package com.ruoyi.yf.test.spider;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//html 解析 工具
public class HtmlJsoup implements HtmlJsoupUtilIn {

    public static void main(String[] args) throws IOException {

        HtmlJsoup hj = new HtmlJsoup();
        Document document = Jsoup.connect("https://book.qidian.com/info/1016751624#Catalog").get();
        hj.getNovelDirectory(document);

////       System.out.println(document);
//        Elements elements = hj.parsingClass(document, "div.volume-wrap > div.volume");
////        System.err.println(">>" + elements);
//
//        elements = hj.delLabelClass(elements, "div.volume:first-child");
//        List<Passage> passages = hj.parsingClassNovelList(elements, "ul.cf > li", "a");
//        System.out.println(">>" + passages.size());
//
//        for (Passage p : passages) {
//            System.out.println(">>" + p);
//        }


    }

    //获取小说目录列表
    public void getNovelDirectory(Document document) {
        HtmlJsoup hj = new HtmlJsoup();
        //所有目录
        Elements elements = hj.parsingClass(document, "div.volume-wrap > div.volume").not("div.volume:first-child");
        //分卷
//        hj.parsingClass(elements, "");
        for (Element element : elements) {
            //卷列表
            System.out.println("12");
            hj.parsingClassNovelList(element,"","");
//            List<Passage> passages = hj.parsingClassNovelList(elements, "ul.cf > li", "a");
        }

    }


    @Override
    public Elements delLabelClass(Elements elements, String cssQuery) {
        return elements.not(cssQuery);
    }

    @Override
    public List<Passage> parsingClassNovelList(Elements elements, String cssQuery) {
        return parsingClassNovelList(elements, cssQuery, "");
    }

    public List<Passage> parsingClassNovelList(Element element, String cssQuery, String volume) {
        Elements elements = element.select(cssQuery);
        return parsingClassNovelList(elements,cssQuery,volume);
    }

    @Override
    public List<Passage> parsingClassNovelList(Elements elements, String cssQuery, String volume) {
        int i = 0;
        List list = new ArrayList();
        for (Element element : elements) {
            i++;
            String title = parsingClass(element, cssQuery);
            String absHref = parsingClassAbsHref(element, cssQuery);
            list.add(new Passage(i, volume, title, absHref));
        }
        return list;
    }


    @Override
    public Elements parsingClass(Document document, String cssQuery) {
        return document.select(cssQuery);
    }

    @Override
    public Elements parsingClass(Elements elements, String cssQuery) {
        return elements.select(cssQuery);
    }

    @Override
    public String parsingClass(Element element, String cssQuery) {
        return element.select(cssQuery).text();
    }

    @Override
    public String parsingClassHref(Element element, String cssQuery) {
        return element.select(cssQuery).attr("href");
    }

    @Override
    public String parsingClassAbsHref(Element element, String cssQuery) {
        return element.select(cssQuery).attr("abs:href");
    }
}
